Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
@thednp/shorty
Advanced tools
@thednp/shorty is a utility library that provides a collection of small, reusable functions for common tasks in JavaScript. It aims to simplify and streamline code by offering concise and efficient solutions for various operations.
String Manipulation
This feature provides functions for manipulating strings, such as converting strings to camel case.
const { toCamelCase } = require('@thednp/shorty');
const result = toCamelCase('hello-world');
console.log(result); // 'helloWorld'
Array Utilities
This feature includes functions for working with arrays, such as removing duplicate elements.
const { unique } = require('@thednp/shorty');
const result = unique([1, 2, 2, 3, 4, 4, 5]);
console.log(result); // [1, 2, 3, 4, 5]
Object Utilities
This feature offers functions for handling objects, such as deep cloning an object.
const { deepClone } = require('@thednp/shorty');
const obj = { a: 1, b: { c: 2 } };
const clonedObj = deepClone(obj);
console.log(clonedObj); // { a: 1, b: { c: 2 } }
Type Checking
This feature provides functions for type checking, such as verifying if a value is a string.
const { isString } = require('@thednp/shorty');
const result = isString('hello');
console.log(result); // true
Lodash is a popular utility library that offers a wide range of functions for manipulating arrays, objects, strings, and more. It is more comprehensive and widely used compared to @thednp/shorty.
Underscore is another utility library that provides a variety of functions for common programming tasks. It is similar to lodash but with a smaller footprint and fewer features.
Ramda is a functional programming library for JavaScript that emphasizes immutability and side-effect-free functions. It offers similar utilities but with a focus on functional programming paradigms.
A small TypeScript library with various tools for increasing your productivity while helping you to create lighter libraries or web components. If there is anything that is consistently repeating itself, shorty can help you save up to 50% of the code required, with little to no performance cost.
shorty is featured in ColorPicker, KUTE.js, BSN, Navbar.js and other libraries.
instanceof
for various objects.dist
folder, that is mainly for build consistency testing. You can make use of "tree shaking" to import one or anything your code needs.src
root folder, the structure is key to understanding the purpose of each type of shortie, whether we have boolean
for various basic browser detection or browser feature support, attr
for all things Element attributes or strings
for most common and most used Element.prototype methods.pnpm install -D @thednp/shorty
yarn add -D @thednp/shorty
npm install -D @thednp/shorty
deno add -D npm:@thednp/shorty@latest
// import the tool you need
import { supportTransform } from "@thednp/shorty";
// use the tool in your ES6/ES7 sources
if (supportTransform()) {
// have modern browsers do something about that
}
// EXAMPLES
import { getAttribute, hasAttribute, setAttribute } from "@thednp/shorty";
// check target has certain attribute
if (!hasAttribute(myTarget, "attribute-name")) {
setAttribute(myTarget, "attribute-name", "new-value");
}
// get attribute value
const currentAttrValue = getAttribute(myTarget, "attribute-name");
document.body
;document.documentElement
;document.head
;boolean
value for the client browser is either Apple Safari browser or not;boolean
value for the client browser is either Firefox or not;boolean
value for the client browser is either a Mobile device or not;boolean
value for the client browser is a WebKit browser or not;boolean
value for the client browser capability for webKit perspective
;boolean
value for the client browser capability for touch
events;boolean
value for the client browser capability for passive
event option;boolean
value for the client browser capability for webKit transform
;boolean
value for the client browser capability for webKit keyframe animation
;boolean
value for the client browser capability for webKit transition
;// EXAMPLES
import { support3DTransform } from "@thednp/shorty";
// filter myAction to supported browsers
if (support3DTransform()) {
// do something with modern browsers
}
// EXAMPLES
import { addClass, removeClass, hasClass } from "@thednp/shorty";
// add a class
addClass(targetElement, "className");
// remove a class
removeClass(targetElement, "className");
// check for a class
if (hasClass(targetElement, "className")) {
// do something about that
}
// EXAMPLES
import { on, off, one, passiveHandler } from "@thednp/shorty";
// attach a passive mousedown eventHandler
on(targetElement, "click", eventHandler, passiveHandler);
// detach a passive mouseup eventHandler
off(targetElement, "mouseup", eventHandler, passiveHandler);
// attach a single instance passive touchstart eventHandler
one(targetElement, "touchstart", eventHandler, passiveHandler);
For a more advanced method to handle event listeners, I recommend using the event-listener.
#Document
for a given Element or just any Document, useful when working with iframes;<body>
for a given Element or just any;<html>
for a given Element or just any;<head>
for a given Element or just any;animationDelay
property of an animation
property;animationDuration
property of a animation
property;transitionDelay
property of a transition
property;transitionDuration
property of a transition
property;{ x, y }
scroll position;offsetParent
that's the best for calculating position for a given target;offsetParent
;{ offsetTop, offsetLeft, offsetWidth, offsetHeight }
object for a SVGElement relative to the closest HTMLElement
;Window
for a given Element or just any Window;// EXAMPLES
import { getElementAnimationDuration } from "@thednp/shorty";
// store the transition duration for target element on a modern browser
const duration = getElementAnimationDuration(target);
Array
;HTMLCanvasElement
instance;CustomElement
instance;Document
instance;Element
instance;Element
is partially visible in the viewport;Element
is fully visible in the viewport;Array
with Element
instances;Function
instance;HTMLCollection
instance;HTMLElement
instance;HTMLImageElement
instance;SVGElement
, HTMLImageElement
, HTMLCanvasElement
or HTMLVideoElement
instance;Node
instance;NodeList
instance;<html dir="rtl">
;ShadowRoot
instance;SVGElement
instance;<table>
, <td>
or <th>
Element;Window
instance;// EXAMPLES
import { isArray, isHTMLElement, isElementsArray } from "@thednp/shorty";
// check if a value is an `Array` of `Element` instances
if (isArray(myValue) && myValue.every(isHTMLElement)) {
// do something with these instances
}
// or use the dedicated shortie of the above
if (isElementsArray(myValue)) {
// do something with these instances
}
Array.from()
method;Map
;Element.dispatchEvent()
method;Array
;animationend
event is triggered, or execute the callback right after for legacy browsers;transitionend
event is triggered, or execute the callback right after for legacy browsers;Float32Array.from()
method;Float64Array.from()
method;Element.focus()
method;() => {}
NOOP;data-NAMESPACE-option="value"
; priority: JavaScript options > DATA API options > default optionsObject.assign()
method;Object.entries()
method;Object.hasOwn()
method;Object.keys()
method;Object.values()
method;CustomEvent
with the added relatedTarget
and other properties;options
with passive: true
event option used;offsetHeight
value, also because using just element.offsetHeight;
won't validate on ESLint;setTimeout
have a meaning;String.toLowerCase()
method;String.toUpperCase()
method;The Data and Timer utilities have their own specifics, you might want to check the wiki.
// EXAMPLES
import { emulateTransitionEnd, distinct } from "@thednp/shorty";
// execute a callback when transitionend is triggered for the target
emulateTransitionEnd(targetElement, callback);
// define some arrays of numbers
const array1 = [0, 1, 3, 5, 7, 9];
const array2 = [0, 2, 4, 6, 8, 10];
// merge them and filter them to make sure we have distinct values
const array3 = [...array1, ...array2].filter(distinct);
// [0, 1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
Element.closest()
method;Array
with all registered CustomElement
;document.getElementById()
method;Element.getElementsByClassName()
method;Element.getElementsByTagName()
method;Element.matches()
method;// EXAMPLES
import { querySelector, querySelectorAll, documentAll, matches } from "@thednp/shorty";
// get first element that matches a certain selector
const element = querySelector(".my-class-name");
// get all elements that matches same selector
const elements = querySelectorAll(".my-class-name");
// now do the same as the above, but differently
const elements = [...documentAll].filter((x) => matches(x, ".my-class-name"));
transition-timing-function
based on Cubic Bezier; EG: cubic-bezier(0.215,0.61,0.355,1)
for bezierEasings.easingCubicOut
;mousedown
, end: mouseup
, move: mousemove
, cancel: mouseout
;mousedown
, up: mouseup
;mouseenter
and mouseleave
OR mouseover
and mouseout
;touchstart
, end: touchend
, move: touchmove
, cancel: touchcancel
;animationDuration
property for modern browsers;animationDelay
property for modern browsers;animationEndEvent
event for modern browsers;animationName
property name for modern browsers;transitionDuration
property name for modern browsers;transitionDelay
property name for modern browsers;transitionend
event name for modern browsers;transitionProperty
property name for modern browsers;addEventListener
method name;removeEventListener
method name;There are lots more string constants available which include native event names, browser strings, keyboard key codes or ARIA specific attribute names. Be sure to check the src/strings
folder for a complete list.
// EXAMPLES
import { on, off, one, mouseClickEvents, touchEvents, passiveHandler } from "@thednp/shorty";
// attach a passive mousedown eventHandler
on(targetElement, mouseClickEvents.down, eventHandler, passiveHandler);
// detach a passive mousedown eventHandler
off(targetElement, mouseClickEvents.down, eventHandler, passiveHandler);
// attach a single instance passive touchstart eventHandler
one(targetElement, touchEvents.start, eventHandler, passiveHandler);
Here's a simple example to showcase the benefit of using shorty.
// This is your typical day to day scripting
const target = document.getElementById("my-element");
const target2 = document.getElementById("my-element2");
target.addEventListener("click", function (e) {
target.classList.add("my-className");
});
target2.addEventListener("mouseenter", function (e) {
target.classList.add("my-other-className");
});
Now make it all shorty.
// Example
import { on, addClass, getElementById, mouseClickEvent, mouseenterEvent } from "@thednp/shorty";
const target = getElementById("my-element");
const target2 = getElementById("my-element2");
on(target, mouseclickEvent, (e) => {
addClass(target, "my-className");
});
on(target2, mouseenterEvent, (e) => {
addClass(target2, "my-other-className");
});
You notice a pattern yet?
shorty is released under the MIT License
FAQs
TypeScript shorties for the web
The npm package @thednp/shorty receives a total of 177,870 weekly downloads. As such, @thednp/shorty popularity was classified as popular.
We found that @thednp/shorty demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.